<?php

namespace App\Admin\Metrics\Examples;

// use Dcat\Admin\Admin;
// use Dcat\Admin\Widgets\ApexCharts\Chart;
use App\Models\BoxIntegralOrder;
use App\Models\BoxGiftOrder;

// class MyBar extends Chart
// {
//     public function __construct($containerSelector = null, $options = [])
//     {
//         parent::__construct($containerSelector, $options);

//         $this->setUpOptions();
//     }

//     /**
//      * 初始化图表配置
//      */
//     protected function setUpOptions()
//     {
//         $color = Admin::color();

//         $colors = [$color->primary(), $color->primaryDarker()];

//         $this->options([
//             'colors' => $colors,
//             'chart' => [
//                 'type' => 'bar',
//                 'height' => 430,
//                 'background' => 'white',
//                 'toolbar' => [
//                     'show' => true,
//                 ],
//                 'scrollingHorizontally' => true, // Enable horizontal scrolling
//             ],
//             'plotOptions' => [
//                 'bar' => [
//                     'horizontal' => false,
//                     'dataLabels' => [
//                         'position' => 'top',
//                     ],
//                 ],
//             ],
//             'dataLabels' => [
//                 'enabled' => true,
//                 'offsetY' => -5,
//                 'style' => [
//                     'fontSize' => '16px',
//                     'fontWeight' => '300',
//                     'fontFamily' => '微软雅黑, sans-serif',
//                     'colors' => ['#fff'],
//                 ],
//             ],
//             'stroke' => [
//                 'show' => true,
//                 'width' => 1,
//                 'colors' => ['#fff'],
//             ],
//             'xaxis' => [
//                 'categories' => [], // Months will be added dynamically
//             ],
//             'title' => [
//                 'text' => '订单统计',
//                 'align' => 'center',
//                 'style' => [
//                     'fontSize' => '16px',
//                     'fontFamily' => '微软雅黑, sans-serif',
//                 ],
//             ],
//             'plotOptions' => [
//                 'bar' => [
//                     'events' => [
//                         'dataPointSelection' => [
//                             'function' => "function (event, chartContext, config) {
//                                 var monthIndex = config.dataPointIndex;
//                                 toggleMonthVisibility(monthIndex);
//                             }",
//                         ],
//                     ],
//                 ],
//             ],
//         ]);
//     }

//     protected function buildData()
//     {
//         // Initialize an array to hold data for each month
//         $data = [];

//         for ($month = 1; $month <= 12; $month++) {
//             $giftOrders = BoxGiftOrder::whereMonth('date', $month)->count();
//             $integralOrders = BoxIntegralOrder::whereMonth('date', $month)->count();
//             $totalOrders = $giftOrders + $integralOrders;

//             $data[] = [
//                 'data' => [$totalOrders === 0 ? null : $totalOrders],
//             ];
//         }

//         $categories = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
//         $this->withData($data);
//         $this->withCategories($categories);
//     }

//     public function withData(array $data)
//     {
//         return $this->option('series', $data);
//     }

//     public function withCategories(array $data)
//     {
//         return $this->option('xaxis.categories', $data);
//     }

//     public function render()
//     {
//         $this->buildData();

//         return parent::render();
//     }
// }


use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\ApexCharts\Chart;

class MyBar extends Chart
{
    public function __construct($containerSelector = null, $options = [])
    {
        parent::__construct($containerSelector, $options);

        $this->setUpOptions();
    }

    /**
     * 初始化图表配置
     */
    protected function setUpOptions()
    {
        $color = Admin::color();
    
        $colors = [$color->primary(), $color->primaryDarker()];
    
        $this->options([
            'colors' => $colors,
            'chart' => [
                'type' => 'bar',
                'height' => 430,
                'background' => '#fff', // 背景颜色
            ],
            'plotOptions' => [
                'bar' => [
                    'horizontal' => false, // 改为竖状图
                    'dataLabels' => [
                        'position' => 'top',
                    ],
                ]
            ],
            'dataLabels' => [
                'enabled' => true,
                'offsetY' => -6,
                'style' => [
                    'fontSize' => '16px', // 字体大小
                    'fontWeight' => '300', // 字体粗细，100表示较细
                    'fontFamily' => '微软雅黑, sans-serif', // 字体
                    'colors' => ['#fff']
                ]
            ],
            'stroke' => [
                'show' => true,
                'width' => 1,
                'colors' => ['#fff']
            ],
            'xaxis' => [
                'categories' => [], // 月份
            ],
            'title' => [
                'text' => '订单统计', // 标题
                'align' => 'center',
                'style' => [
                    'fontWeight' => '250',
                    'fontSize' => '20', // 标题字体大小
                    'fontFamily' => '微软雅黑, sans-serif', // 标题字体
                ]
            ],
        ]);
    }

    /**
     * 处理图表数据
     */
    protected function buildData()
    {
        // 执行你的数据查询逻辑
        $data = [
            [
                'name' => '礼盒', // 指定系列名称为礼盒
                'data' => [], // 存储 box_gift_order 数据
            ],
            [
                'name' => '积分', // 指定系列名称为礼
                'data' => [], // 存储 box_integral_order 数据
            ],
        ];
    
        $categories = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
    
        // 查询每个月的订单数据并填充到 $data 数组中
        for ($month = 1; $month <= 12; $month++) {
            $giftOrders = BoxGiftOrder::whereMonth('date', $month)->count();
            $integralOrders = BoxIntegralOrder::whereMonth('date', $month)->count();
    
            $data[0]['data'][] = $giftOrders;
            $data[1]['data'][] = $integralOrders;
        }
    
        $this->withData($data);
        $this->withCategories($categories);
    }

    /**
     * 设置图表数据
     *
     * @param array $data
     *
     * @return $this
     */
    public function withData(array $data)
    {
        return $this->option('series', $data);
    }

    /**
     * 设置图表类别.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withCategories(array $data)
    {
        return $this->option('xaxis.categories', $data);
    }

    /**
     * 渲染图表
     *
     * @return string
     */
    public function render()
    {
        $this->buildData();

        return parent::render();
    }
}